package eu.runelord.wjug.singleton;

import java.util.LinkedList;
import java.util.List;

public class Example3 {

	public static void main(String[] args) {

	}

}

class Pool {
	private final long maxpool;
	private final long minpool;

	private final List<ISingleton> pool;
	private int current = -1;

	public Pool(long maxpool, long minpool) {
		if (minpool > maxpool || maxpool < 0)
			throw new IllegalArgumentException(
					"maxpool mniejsze od 0 lib od minpool");
		this.maxpool = maxpool;
		this.minpool = minpool;
		
		this.pool = new LinkedList<ISingleton>();
		generatePool();
	}

	private void generatePool() {
		for (int i = 0; i < minpool; i++) {
			pool.add(SingletonFactory.getINSTANCE());
		}
	}

	public ISingleton get() {
		current++;
		if(current>maxpool)
			throw new RuntimeException("Too many objects!");
		if(current==pool.size())
			pool.add(current, SingletonFactory.getINSTANCE());
		if(pool.get(current) == null)
			pool.add(current, SingletonFactory.getINSTANCE());
		return pool.get(current);
	}

	public void ret(ISingleton singleton) {
		pool.remove(singleton);
		current--;
	}
}